package com.mallify.admin.controller;

import com.mallify.common.page.CommonPage;
import com.mallify.common.request.PageParamRequest;
import com.mallify.common.request.StoreProductAddRequest;
import com.mallify.common.request.StoreProductSearchRequest;
import com.mallify.common.response.Result;
import com.mallify.common.response.StoreProductInfoResponse;
import com.mallify.common.response.StoreProductResponse;
import com.mallify.common.response.StoreProductTabsHeader;
import com.mallify.service.service.StoreCartService;
import com.mallify.service.service.StoreProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品表 前端控制器
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/store/product")
@Api(tags = "商品")
public class StoreProductController {

    @Resource
    private StoreProductService storeProductService;

    @Resource
    private StoreCartService storeCartService;

    /**
     * 分页显示商品表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:product:list')")
    @ApiOperation(value = "分页列表")
    @GetMapping("/list")
    public Result<CommonPage<StoreProductResponse>> getList(@Validated StoreProductSearchRequest request,
                                                            @Validated PageParamRequest pageParamRequest) {
        return Result.success(CommonPage.restPage(storeProductService.getAdminList(request, pageParamRequest)));
    }

    /**
     * 新增商品
     *
     * @param request 新增参数
     */
    @PreAuthorize("hasAuthority('admin:product:save')")
    @ApiOperation(value = "新增商品")
    @PostMapping("/save")
    public Result<String> save(@RequestBody @Validated StoreProductAddRequest request) {
        if (Boolean.TRUE.equals(storeProductService.save(request))) {
            return Result.success();
        } else {
            return Result.failed();
        }
    }

    /**
     * 删除商品表
     *
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:product:delete')")
    @ApiOperation(value = "删除商品")
    @GetMapping("/delete/{id}")
    public Result<String> delete(@RequestBody @PathVariable Integer id, @RequestParam(value = "type", required = false, defaultValue = "recycle") String type) {
        // type 参数用于指定删除商品的操作类型。根据不同的类型，系统可能会执行不同的逻辑。在你的代码中，type 参数有两个可能的值：
        // recycle：表示将商品放入回收站（软删除）。
        // 其他值（如 delete）：表示直接删除商品（硬删除）。
        // 通过 type 参数，接口可以支持多种删除方式，从而满足不同的业务需求。
        if (Boolean.TRUE.equals(storeProductService.deleteProduct(id, type))) {
            if ("recycle".equals(type)) {
                storeCartService.productStatusNotEnable(id);
            } else {
                storeCartService.productDelete(id);
            }
            return Result.success();
        } else {
            return Result.failed();
        }
    }

    /**
     * 恢复已删除商品表
     *
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:product:restore')")
    @ApiOperation(value = "恢复商品")
    @GetMapping("/restore/{id}")
    public Result<String> restore(@RequestBody @PathVariable Integer id) {
        if (Boolean.TRUE.equals(storeProductService.reStoreProduct(id))) {
            return Result.success();
        } else {
            return Result.failed();
        }
    }

    /**
     * 商品修改
     *
     * @param storeProductRequest 商品参数
     */
    @PreAuthorize("hasAuthority('admin:product:update')")
    @ApiOperation(value = "商品修改")
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated StoreProductAddRequest storeProductRequest) {
        if (Boolean.TRUE.equals(storeProductService.update(storeProductRequest))) {
            return Result.success();
        } else {
            return Result.failed();
        }
    }

    /**
     * 商品详情
     *
     * @param id 商品id
     */
    @PreAuthorize("hasAuthority('admin:product:info')")
    @ApiOperation(value = "商品详情")
    @GetMapping("/info/{id}")
    public Result<StoreProductInfoResponse> info(@PathVariable Integer id) {
        return Result.success(storeProductService.getInfo(id));
    }

    /**
     * 商品tabs表头数据
     */
    @PreAuthorize("hasAuthority('admin:product:tabs:headers')")
    @ApiOperation(value = "商品表头数量")
    @GetMapping("/tabs/headers")
    public Result<List<StoreProductTabsHeader>> getTabsHeaders() {
        return Result.success(storeProductService.getTabsHeader());
    }

    /**
     * 上架
     */
    @PreAuthorize("hasAuthority('admin:product:up')")
    @ApiOperation(value = "上架")
    @GetMapping("/putOnShell/{id}")
    public Result<String> putOn(@PathVariable Integer id) {
        if (Boolean.TRUE.equals(storeProductService.putOnShelf(id))) {
            return Result.success();
        } else {
            return Result.failed();
        }
    }

    /**
     * 下架
     */
    @PreAuthorize("hasAuthority('admin:product:down')")
    @ApiOperation(value = "下架")
    @GetMapping("/offShell/{id}")
    public Result<String> offShell(@PathVariable Integer id) {
        if (Boolean.TRUE.equals(storeProductService.offShelf(id))) {
            return Result.success();
        } else {
            return Result.failed();
        }
    }
}
